package com.ita.httpclient;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.ita.dto.VerifyDto;
import com.ita.util.JacksonUtil;
import com.ita.util.PropertiesUtil;

public class CheckClient {

	private final static String GET_VERIFY_PATH = "GET_VERIFY_PATH";
	private static Logger logger = Logger.getLogger(CheckClient.class);
	private URL url;
	private HttpURLConnection hcon;
	private Properties prop;

	public CheckClient(String id) throws IOException {
		prop = PropertiesUtil.getProp();
		url = new URL(prop.getProperty(GET_VERIFY_PATH).replace("mid", id));
	}

	public VerifyDto getInfo() throws IOException {
		VerifyDto vd = null;
		try {

			hcon = (HttpURLConnection) url.openConnection();
			// hcon.setDoInput(true);
			// hcon.setDoOutput(true);
			hcon.setRequestMethod("GET");
			// hcon.setRequestProperty("header-key", "xxx");
			InputStream in = hcon.getInputStream();
			byte[] buf = new byte[in.available()];
			in.read(buf);
			vd= JacksonUtil.getObjectFromJson(new String(buf), VerifyDto.class);
			in.close();
			hcon.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("remote Admin side has something error");
			return vd;
		}
		return vd;
	}

}
